﻿using UnityEngine;
using System.Collections;

public class Character : MonoBehaviour
{
    public AudioSource walkAudio;

    private CharacterController controller;
    public Animator animator;
    public CharacterModel character;
    public Vector3 firstPos;
    //private Vector3 cameraPos;
    private Vector3 moveDirection = Vector3.zero;

    static int idle_cState = Animator.StringToHash("Base Layer.Idle_C");
    static int idle_aState = Animator.StringToHash("Base Layer.Idle_A");
    static int locoState = Animator.StringToHash("Base Layer.Locomotion");
    static int jumpState = Animator.StringToHash("Base Layer.Jump");
    static int cute1State = Animator.StringToHash("Base Layer.Cute1");

    public float joyPositionX;
    public float joyPositionY;

    private bool jump;
    private bool canKey;

    void OnDestroy()
    {
        EventManager.RemoveListener(Character.DataType.Jump, Jump);
        EventManager.RemoveListener(DataType.PlayAudio, PlayAudio);
    }

    void Start()
    {
        firstPos = transform.position;

        EventManager.AddListener(Character.DataType.Jump, Jump);
        EventManager.AddListener(DataType.PlayAudio, PlayAudio);
        character = new CharacterModel();
        controller = GetComponent<CharacterController>();
        animator = GetComponent<Animator>();
        //cameraPos = Camera.main.transform.position - transform.position;

        SmoothFollow follow = Camera.main.gameObject.AddComponent<SmoothFollow>();
        follow.target = this.transform;

        GameObject.Find("MoveJoystick").GetComponent<MoveController>().character = this;

        //角色创建完毕最后交给UI界面处理
        EventManager.PostEvent(GameUI.DataType.GetCharacter, this);
    }

    void Update()
    {
        if (transform.position.y < -10)
        {
            EventManager.PostEvent(GameUI.DataType.GameOver);
        }

        float h = 0;
        float v = 0;

        if (joyPositionY != 0 || joyPositionX != 0)
        {
            h = joyPositionX;
            v = joyPositionY;
        }
        else
        {
            h = Input.GetAxis("Horizontal");
            v = Input.GetAxis("Vertical");
        }

        float max = Mathf.Max(Mathf.Abs(h), Mathf.Abs(v));
        animator.SetFloat("Speed", max);

        AnimatorStateInfo currentBaseState = animator.GetCurrentAnimatorStateInfo(0);

        if (currentBaseState.fullPathHash == cute1State)
        {
            if (!animator.IsInTransition(0))
            {
                animator.SetBool("Cute1", false);
            }
        }
        else
        {
            if (controller.isGrounded)
            {
                moveDirection = Vector3.zero;
                //Camera.main.transform.LookAt(transform);
            }
            else
            {
                moveDirection.y -= character.gravity * Time.deltaTime;
            }

            Vector3 target = new Vector3(h, 0, v);
            transform.LookAt(target + transform.position);

            if (max > 0.1f)
            {
                moveDirection.x = transform.forward.x * character.speed;
                moveDirection.z = transform.forward.z * character.speed;
            }
        }

        if (currentBaseState.fullPathHash == idle_aState || currentBaseState.fullPathHash == idle_cState)
        {
            walkAudio.Pause();
            if (Input.GetButtonDown("Jump") || jump)
            {
                animator.SetBool("Cute1", true);
                jump = false;
            }
        }
        else if (currentBaseState.fullPathHash == locoState)
        {
            if (!walkAudio.isPlaying)
            {
                walkAudio.Play();
            }
            if (Input.GetButtonDown("Jump") || jump)
            {
                animator.SetBool("Jump", true);
                character.speed = 1f;
                moveDirection.y = character.jumpSpeed;
                jump = false;
            }
        }
        else if (currentBaseState.fullPathHash == jumpState)
        {
            walkAudio.Pause();
            if (Input.GetButtonDown("Jump") || jump)
            {
                animator.SetBool("Jump", false);
                moveDirection.y = character.jumpSpeed * 1.2f;
                animator.SetBool("Jump", true);
                jump = false;
            }

            if (!animator.IsInTransition(0))
            {
                animator.SetBool("Jump", false);
                character.speed = 1.5f;
            }


        }

        controller.Move(moveDirection * Time.deltaTime);
        //Camera.main.transform.position = cameraPos + new Vector3(transform.position.x, 0, transform.position.z);
    }

    public void PlayAudio(object[] parameters)
    {
        string name = (string)parameters[0];
        bool loop = (bool)parameters[1];
        AudioSource source = GetComponent<AudioSource>();
        source.clip = Resources.Load<AudioClip>("Audio/" + name);
        source.loop = loop;
        source.Play();
    }

    public void Jump(object[] parameters)
    {
        jump = true;
    }

    public enum DataType
    {
        PlayAudio,
        Jump,
        Key
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.name.Equals("OwlCage"))
        {
            EventManager.PostEvent(DataType.PlayAudio, "owl", false);
            EventManager.PostEvent(GameUI.DataType.OnKey, true, other.gameObject);
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.name.Equals("OwlCage"))
        {
            EventManager.PostEvent(GameUI.DataType.OnKey, false);
        }
    }
}
